﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;

namespace TxJsq
{
    class Crack
    {
        internal static void UnPack(string pan)
        {
            String pack = @"pack.zip";
            File.WriteAllBytes(pack, Properties.Resources.TxJsq);
            ZipFile.ExtractToDirectory(pack, pan + @":\");
            File.Delete(pack);
        }
        internal static string DisUpdate(string pan)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.AutoFlush = true;
            p.StandardInput.WriteLine("netsh advfirewall firewall del rule dir=in name=腾讯网游加速器-升级程序");
            p.StandardInput.WriteLine("netsh advfirewall firewall del rule dir=in name=disable_TxJsq_update");
            p.StandardInput.WriteLine("netsh advfirewall firewall add rule dir=in name=disable_TxJsq_update action=block program=" + pan + @":\TxJsq\update\QMProxyAccUpdate.exe");
            p.StandardInput.WriteLine("netsh advfirewall firewall del rule dir=out name=disable_TxJsq_update");
            p.StandardInput.WriteLine("netsh advfirewall firewall add rule dir=out name=disable_TxJsq_update action=block program=" + pan + @":\TxJsq\update\QMProxyAccUpdate.exe");
            p.StandardInput.WriteLine("exit");
            string result = p.StandardOutput.ReadToEnd().ToString();
            p.WaitForExit();
            p.Close();
            return result;
        }
        internal static void KillUpdate()
        {
            Process[] ps = Process.GetProcessesByName("QMProxyAccUpdate");
            foreach (Process p in ps)
            {
                try { p.Kill(); } catch { }
            }
        }
        internal static void StartJsq(string jsq)
        {
            if (Process.GetProcessesByName("QMProxyAcceler").Length == 0)
            {
                Process.Start(jsq, "/entry=InstallDir");
            }
        }
        internal static string Injection()
        {
            Process[] ps = Process.GetProcessesByName("QMProxyAcceler");
            if (ps.Length == 0)
            {
                return "没有找到QMProxyAcceler进程！";
            }
            Process p = ps[0];

            IntPtr processHandle = NativeMethods.OpenProcess(2035711u, false, p.Id);
            if (processHandle == IntPtr.Zero)
            {
                return "打开进程QMProxyAcceler失败！";
            }

            IntPtr baseAddress = p.MainModule.BaseAddress;

            WriteInt(processHandle, baseAddress, 1139712, 1);
            WriteInt(processHandle, baseAddress, 1151216, 2);
            WriteInt(processHandle, baseAddress, 1152304, 0);
            WriteInt(processHandle, baseAddress, 1152312, 3);
            WriteInt(processHandle, baseAddress, 1152316, GetLongTime());
            WriteInt(processHandle, baseAddress, 1153660, 1);

            NativeMethods.CloseHandle(processHandle);

            return "已破解，请点击头像查看到期时间！";
        }
        static long GetLongTime()
        {
            DateTime current = new DateTime(2099, 12, 12, 12, 12, 12);
            DateTime dt0 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return (current.Ticks - dt0.Ticks) / 10000 / 1000;
        }
        static void WriteInt(IntPtr processHandle, IntPtr baseAddress, int offset, long value)
        {
            byte[] bytes = BitConverter.GetBytes(value);
            NativeMethods.WriteProcessMemory(processHandle, IntPtr.Add(baseAddress, offset), bytes, new IntPtr(bytes.Length), IntPtr.Zero);
        }
    }
}
